﻿namespace WebApi2.WebApi
{
    public static class PicCaptureSimulation
    {
        public static RouteGroupBuilder MapCaptureApiV1(this IEndpointRouteBuilder app)
        {
            var api = app.MapGroup("onvif-http").HasApiVersion(1.0);//HasApiVersion和NewVersionedApi要配合使用
            api.MapGet("snapshot", GetPicFile);
            api.MapGet("test", GetSampleObject3);
            return api;
        }

        public static async Task<Ok<byte[]>> GetPic(HttpContext context)
        {
            await Task.CompletedTask;
            return TypedResults.Ok(new byte[] { 2, 3, 4 });
        }

        public static Results<NotFound, PhysicalFileHttpResult> GetPicFile()
        {
            var dir = @"C:\Users\Guoxudong\OneDrive\图片\戴德跑步";
            var files = Directory.GetFiles(dir);
            var file = files[new Random().Next(files.Length)];
            return TypedResults.PhysicalFile(file, "image/jpeg", lastModified: DateTimeOffset.Parse("2021-1-1 12:00:00"));
        }



        public static List<Sample> GetSampleObject() {
            return [
                new() { Id=2, Name="张三" },
                new() { Id=4, Name="李四" }
            ];
        }
        public static Ok<List<Sample>> GetSampleObject2()
        {
            return TypedResults.Ok(new List<Sample> {
                new() { Id=2, Name="张三2" },
                new() { Id=4, Name="李四2" }
            });
        }
        public static Results<Ok<List<Sample>>, NotFound, BadRequest<string>> GetSampleObject3(int p)
        {
            if (p == 1) return TypedResults.NotFound();
            else if (p == 2) return TypedResults.BadRequest("请不要指定参数");
            return TypedResults.Ok(new List<Sample> {
                new() { Id=2, Name="张三3" },
                new() { Id=4, Name="李四3", Birthday = DateTime.Now }
            });
        }
    }

    public class Sample
    {
        public int Id { get; set; }
        public required string Name { get; set; }
        public DateTime Birthday { get; set; }
    }
}
